package com.maila.xiaoaoschool.fragment;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.transition.AutoTransition;
import android.transition.TransitionManager;
import android.transition.TransitionSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.cjj.MaterialRefreshLayout;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.activity.AllFoodsActivity;
import com.maila.xiaoaoschool.activity.ExpressActivity;
import com.maila.xiaoaoschool.activity.LocationActivity;
import com.maila.xiaoaoschool.activity.SearchActivity;
import com.maila.xiaoaoschool.activity.ShopDetailsActivity;
import com.maila.xiaoaoschool.adapter.MainLocationAdapter;
import com.maila.xiaoaoschool.adapter.MainUAdapter;
import com.maila.xiaoaoschool.util.StartActivityUtils;
import com.maila.xiaoaoschool.widget.NoScrollGridView;
import com.maila.xiaoaoschool.widget.NoScrollListView;
import com.maila.xiaoaoschool.widget.UPMarqueeView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2017/9/23.
 */

public class LocationFragment extends Fragment {


    Unbinder unbinder;
    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    @BindView(R.id.all_meishi)
    AutoLinearLayout allMeishi;
    @BindView(R.id.all_chaoshi)
    AutoLinearLayout allChaoshi;
    @BindView(R.id.all_kuaidi)
    AutoLinearLayout allKuaidi;
    @BindView(R.id.all_ziying)
    AutoLinearLayout allZiying;
    @BindView(R.id.laba)
    ImageView laba;
    @BindView(R.id.upview1)
    UPMarqueeView upview1;
    @BindView(R.id.gv_youxuan)
    NoScrollGridView gvYouxuan;
    @BindView(R.id.rb_zh)
    RadioButton rbZh;
    @BindView(R.id.rb_xl)
    RadioButton rbXl;
    @BindView(R.id.rb_jl)
    RadioButton rbJl;
    @BindView(R.id.lv_fujin)
    NoScrollListView lvFujin;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.hot_refresh)
    MaterialRefreshLayout hotRefresh;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.ll_location)
    AutoLinearLayout llLocation;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.ll_search)
    AutoLinearLayout mSearchLayout;
    @BindView(R.id.toolbar)
    AutoRelativeLayout toolbar;
    private TransitionSet mSet;
    boolean isExpand = false;
    MainLocationAdapter locationAdapter;
    MainUAdapter mainUAdapter;
    public static LocationFragment newInstance(String param1) {
        LocationFragment fragment = new LocationFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    public LocationFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_location, container, false);
        unbinder = ButterKnife.bind(this, view);
        locationAdapter=new MainLocationAdapter(this.getActivity());
        mainUAdapter=new MainUAdapter(this.getActivity());
        gvYouxuan.setAdapter(mainUAdapter);
        lvFujin.setAdapter(locationAdapter);
//        //设置toolbar初始透明度为0
//        toolbar.getBackground().mutate().setAlpha(0);
        //scrollview滚动状态监听
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                //改变toolbar的透明度
//                changeToolbarAlpha();
                //滚动距离>=大图高度-toolbar高度 即toolbar完全盖住大图的时候 且不是伸展状态 进行伸展操作
//                if (scrollView.getScrollY() >= convenientBanner.getHeight() - toolbar.getHeight() && !isExpand) {
////                    expand();
//                    isExpand = true;
//                }
                //滚动距离<=0时 即滚动到顶部时  且当前伸展状态 进行收缩操作
//                else if (scrollView.getScrollY() <= 0 && isExpand) {
////                    reduce();
//                    isExpand = false;
//                }
            }
        });
        AutoUtils.autoSize(view);
        return view;
    }

    private void changeToolbarAlpha() {
        int scrollY = scrollView.getScrollY();
        //快速下拉会引起瞬间scrollY<0
        if (scrollY < 0) {
//            toolbar.getBackground().mutate().setAlpha(0);
            return;
        }
//        //计算当前透明度比率
//        float radio = Math.min(1, scrollY / (convenientBanner.getHeight() - toolbar.getHeight() * 1f));
        //设置透明度
//        toolbar.getBackground().mutate().setAlpha((int) (radio * 0xFF));
    }


    private void expand() {
        //设置伸展状态时的布局
        tvSearch.setText("搜索简书的内容和朋友");
        RelativeLayout.LayoutParams LayoutParams = (RelativeLayout.LayoutParams) mSearchLayout.getLayoutParams();
        LayoutParams.width = LayoutParams.MATCH_PARENT;
        LayoutParams.setMargins(dip2px(10), dip2px(10), dip2px(10), dip2px(10));
        mSearchLayout.setLayoutParams(LayoutParams);
        //开始动画
        beginDelayedTransition(mSearchLayout);
    }

    private void reduce() {
        //设置收缩状态时的布局
        tvSearch.setText("搜索");
        RelativeLayout.LayoutParams LayoutParams = (RelativeLayout.LayoutParams) mSearchLayout.getLayoutParams();
        LayoutParams.width = dip2px(80);
        LayoutParams.setMargins(dip2px(10), dip2px(10), dip2px(10), dip2px(10));
        mSearchLayout.setLayoutParams(LayoutParams);
        //开始动画
        beginDelayedTransition(mSearchLayout);
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    void beginDelayedTransition(ViewGroup view) {
        mSet = new AutoTransition();
        mSet.setDuration(300);
        TransitionManager.beginDelayedTransition(view, mSet);
    }

    private int dip2px(float dpVale) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpVale * scale + 0.5f);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.all_meishi, R.id.all_chaoshi, R.id.all_kuaidi, R.id.all_ziying, R.id.upview1, R.id.rb_zh, R.id.rb_xl, R.id.rb_jl, R.id.ll_location, R.id.ll_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.all_meishi:
                StartActivityUtils.start(getActivity(), AllFoodsActivity.class);
                break;
            case R.id.all_chaoshi:
                StartActivityUtils.start(getActivity(), ShopDetailsActivity.class);
                break;
            case R.id.all_kuaidi:
                StartActivityUtils.start(getActivity(), ExpressActivity.class);
                break;
            case R.id.all_ziying:
                StartActivityUtils.start(getActivity(), ShopDetailsActivity.class);
                break;
            case R.id.upview1:
                break;
            case R.id.rb_zh:
                reset_rb();
                rbZh.setChecked(true);
                break;
            case R.id.rb_xl:
                reset_rb();
                rbXl.setChecked(true);
                break;
            case R.id.rb_jl:
                reset_rb();
                rbJl.setChecked(true);
                break;
            case R.id.ll_location:
                StartActivityUtils.start(getActivity(),LocationActivity.class);
                break;
            case R.id.ll_search:
                StartActivityUtils.start(getActivity(),SearchActivity.class);
                break;
        }
    }

    private void reset_rb() {
        rbJl.setChecked(false);
        rbXl.setChecked(false);
        rbZh.setChecked(false);
    }
}